import moment from 'moment'
import httpFetch from 'share/httpFetch';
import errorMessage from 'share/errorMessage';

export default {
  paymentDetails: {},
  applicant: {},
  // 新建付款申请单
  newPaymentRequest(params) {
    return httpFetch.post(`/api/payment/request`, params)
      .catch(err => {
        errorMessage(err.response);
        throw err;
      });
  },
  // 获取付款申请单详情
  getPaymentRequest(docOID) {
    return httpFetch.get(`/api/payment/request/${docOID}`)
      .catch(err => {
        errorMessage(err.response);
        throw err;
      });
  },
  // 获取付款申请单列表
  getPaymentList(params) {
    return httpFetch.get(`/api/payment/request/search/my`, params)
      .catch(err => {
        errorMessage(err.response);
        throw err;
      });
  },
  // 深处付款申请单
  deletePayment(docOID) {
    return httpFetch.delete(`/api/payment/request/${docOID}`)
      .catch(err => {
        errorMessage(err.response);
        throw err;
      });
  },
  // 获取付款申请单审批历史
  getPaymentHistory(params) {
    return httpFetch.get(`/api/payment/request/approval/history`, params)
      .catch(err => {
        errorMessage(err.response);
        throw err;
      });
  },
  // 获取付款申请单付款信息
  getPaymentPayInfo(params, billOID) {
    return httpFetch.get(`/api/payment/request/lines/${billOID}`, params)
      .catch(err => {
        errorMessage(err.response);
        throw err;
      });
  },
  // 提交付款申请单
  submitPaymentRequest(billOID) {
    return httpFetch.post(`/api/payment/request/submit/${billOID}`)
      .catch(err => {
        errorMessage(err.response);
        throw err;
      });
  },
  // 获取供应商银行账户
  // { companyOid, companyId, venNickname }
  getVendorAccount(params) {
    return httpFetch.get(`/supplier/api/ven/info/by/company`, params)
      .catch(err => {
        errorMessage(err.response);
        throw err;
      });
  },
  // 增加付款申请单行信息
  addPaymentPayLine(params, documentOID, id) {
    return new Promise((resolve,reject) => {
      const { businessCode } = params;
      const {  payeeCode, contractHeaderId, id: expPaymentScheduleId } = businessCode[0];
      const {payeeType, payeeBankAccountNumber,payeeName, cshTransactionClassId, realPaymentAmount, planedPaymentDate, remark, payeeBankAccountName, paymentMethod } = params;
      return httpFetch.post(`/api/payment/request/line/${documentOID}`, {
        expPaymentScheduleId,
        payeeType,
        payeeCode,
        payeeName,
        payeeBankAccountName,
        paymentMethod,
        contractHeaderId,
        payeeBankAccountNumber,
        cshTransactionClassId,
        planedPaymentDate,
        remark,
        ...realPaymentAmount,
        id
      })
        .then(res => {
          resolve(true);
        })
        .catch(err => {
          errorMessage(err.response);
          reject(err);
          throw err;
        });
    });
  },
// 删除行信息
  deletePaymentPayLine(lineId){
    return httpFetch.delete(`/api/payment/request/line/${lineId}`)
      .catch(err => {
        errorMessage(err.response);
        throw err;
      });
  },
  // 获取表单信息
  getFormDetail(billOID) {
    return httpFetch.get(`/api/payment/request/${billOID}/form`)
      .catch(err => {
        errorMessage(err.response);
        throw err;
      });
  },
  // 获取行详情
  getPayInfoDetails(lineId) {
    return new Promise((resolve, reject) => {
      httpFetch.get(`/api/payment/request/line/${lineId}`)
        .then(res => {
          const line = res.data;
          const { expPaymentScheduleId:id, payeeType, payeeCode, payeeName, payeeBankAccountName,
            payeeBankAccountNumber, paymentMethod, cshTransactionClassId, paymentCurrencyCode,
            amount, planedPaymentDate, contractHeaderId, remark, expBusinessCode,
            associatedAmount, availableAmount, contractNumber, payeeId } = line;

          const businessCode = [{
            id,
            payeeCode,
            paymentMethod,
            contractHeaderId,
            availableAmount,
            associatedAmount,
            businessCode: expBusinessCode,
            payeeType,
            payeeName,
            accountNumber: payeeBankAccountNumber,
            accountName: payeeBankAccountName,
            cshTransactionClassId,
            planedPaymentDate,
            remark,
            payeeId
          }];
          const realPaymentAmount = { amount, paymentCurrencyCode };
          resolve({
            businessCode,
            realPaymentAmount,
            payeeType: payeeType + '',
            payeeName,
            payeeBankAccountName,
            payeeBankAccountNumber,
            cshTransactionClassId,
            paymentMethod,
            contractHeaderId: contractNumber,
            planedPaymentDate: planedPaymentDate ? moment(new Date(planedPaymentDate), 'YYYY-MM-DD') : void 0,
            remark
          });
        })
        .catch(err => {
          errorMessage(err.response);
          reject(err);
          throw err;
        });
    });
  }
};
